package db

import (
	"d-serv-go/server/core/bean"
	strutils "d-serv-go/server/core/util/stringutils"
	"strconv"
)

type MysqlExecutor struct {
	SqlExecutor
}

func NewExecutor() *MysqlExecutor {
	return &MysqlExecutor{
		SqlExecutor{},
	}
}

func (c *MysqlExecutor) QueryPage(sql string, pageNum int, count int, values []interface{}, call Callback) ([]*bean.Bean, error) {
	if count > 0 { // 分页处理
		pageSql := strutils.NewStringBuilder("")
		if values != nil { // psql
			pageSql.Append(sql).Append(" limit ?,?")
			if pageNum <= 0 {
				values = append(values, pageNum*count)
			} else {
				values = append(values, (pageNum-1)*count)
			}
			values = append(values, count)
		} else {
			pageSql.Append(sql).Append(" limit ")
			if pageNum <= 0 {
				pageSql.Append(strconv.Itoa(pageNum * count))
			} else {
				pageSql.Append(strconv.Itoa((pageNum - 1) * count))
			}
			pageSql.Append(",").Append(strconv.Itoa(count))
		}
		sql = pageSql.ToString()
	}
	return c.QueryCall(sql, values, call)
}
